#include "overviewscreen.h"

int textlayeroscreen =2;

void initoscreen(){
	NF_CreateTiledBg(	1,		// screen
						3,		// layer 0-3
						"bgmenu"	// name
						);

	NF_LoadTextFont("default",	// file name (.FNT)
					"downoscreen",	// name
					256,
					256,
					0			// rotation
					);
	NF_CreateTextLayer(1,		// screen 0-1
					textlayeroscreen,			// layer 0-3
					0,			// rotation 0-2
					"downoscreen"	// name
					);
	//NF_ClearTextLayer(1,textlayeroscreen);


	NF_CreateSprite(1, //screen
				4, //sprite id 0-127
				4, //gfx
				1, //palette
				200, 136);//x,y
	
	NF_CreateSprite(1, //screen
				5, //sprite id 0-127
				5, //gfx
				1, //palette
				24, 136);//x,y
	
	NF_HflipSprite(1,4,true);//screen, sprite,boolvflip

	NF_ShowSprite(1,4,false);
	NF_ShowSprite(1,5,false);


}


void displayFleet(Fleet f){
	vector<string> ships;
	unsigned int j;
	const char * mission = f.getMission();
	const char * source = f.getSource();
	const char * dest = f.getDestination();
	const char * it = f.getImpacttime();
	const char * sc = f.getSecondleft();
	NF_WriteText(1, textlayeroscreen, 2, 1, mission);
	NF_WriteText(1, textlayeroscreen, 2, 2, "source : ");
	NF_WriteText(1, textlayeroscreen, 16, 2, source );
	NF_WriteText(1, textlayeroscreen, 2, 3, "destination : ");
	NF_WriteText(1, textlayeroscreen, 17, 3, dest );
	NF_WriteText(1, textlayeroscreen, 2, 4, "time left : ");
	NF_WriteText(1, textlayeroscreen, 17, 4, sc );
	NF_WriteText(1, textlayeroscreen, 2, 5, "arrival date : ");
	NF_WriteText(1, textlayeroscreen, 17, 5, it );
	NF_WriteText(1, textlayeroscreen, 2, 6, "ships : ");
	ships=f.getShips();
	for(j=0;j<ships.size();j++){
		const char *p;
		p=ships[j].c_str();
		NF_WriteText(1, textlayeroscreen, 4, 7 + j, p);
	}
	NF_UpdateTextLayers();
}

int arrowTouchedoscreen(){
	touchPosition touch;
	touchRead(&touch);
	int x = touch.px;
	int y = touch.py;
		if(y >= 136 && y <= 168){
			if(x >= 200 &&  x <= 232 ){
				return 2;
			}
			if(x >= 24 &&  x <= 56 ){
				return 1;
			}
		}
			
	return 0;
}

void runoscreen (OverviewPage * op){
	initoscreen();
	const vector<Fleet> fleets = op->getFleets();
	int touch,cpt,max;
	cpt=0;
	max = fleets.size();
	if( max > 0)
		displayFleet(fleets[cpt]);
	while(1){
		if(cpt>0)
			NF_ShowSprite(1,5,true);
		else
			NF_ShowSprite(1,5,false);
		if(cpt< (max - 1) )
			NF_ShowSprite(1,4,true);
		else
			NF_ShowSprite(1,4,false);
		NF_SpriteOamSet(0);
		NF_SpriteOamSet(1);
		// Actualiza el OAM
		oamUpdate(&oamMain);
		oamUpdate(&oamSub);
		swiWaitForVBlank();		// Espera al sincronismo vertical

		scanKeys();	
		int pressed = keysDown();
		if ( !(pressed & KEY_TOUCH) ) continue;
		
		touch = arrowTouchedoscreen();
		if(touch == 1 && cpt >0){
			cpt--;
			displayFleet(fleets[cpt]);	
		}
		if(touch == 2 && cpt < max){
			cpt++;
			displayFleet(fleets[cpt]);	
		}


		
	}	
	
}